package com.dreamlin.gankio.mvvm.home.other

import androidx.recyclerview.widget.DiffUtil
import com.dreamlin.gankio.mvvm.home.GankEntity

class GankEntityDiffCallback() : DiffUtil.Callback() {

    private lateinit var oldList: List<GankEntity>
    private lateinit var newList: List<GankEntity>

    constructor(
        oldEntities: List<GankEntity>,
        newEntities: List<GankEntity>
    ) : this() {
        oldList = oldEntities
        newList = newEntities
    }

    fun setOldAndNewData(
        oldEntities: List<GankEntity>,
        newEntities: List<GankEntity>
    ) {
        oldList = oldEntities
        newList = newEntities
    }

    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return oldList[oldItemPosition].id == newList[newItemPosition].id
    }

    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return oldList[oldItemPosition].author == newList[newItemPosition].author &&
                oldList[oldItemPosition].category == newList[newItemPosition].category &&
                oldList[oldItemPosition].likeCounts == newList[newItemPosition].likeCounts &&
                oldList[oldItemPosition].views == newList[newItemPosition].views &&
                oldList[oldItemPosition].stars == newList[newItemPosition].stars &&
                oldList[oldItemPosition].type == newList[newItemPosition].type &&
                oldList[oldItemPosition].title == newList[newItemPosition].title &&
                oldList[oldItemPosition].url == newList[newItemPosition].url &&
                oldList[oldItemPosition].images.size == newList[newItemPosition].images.size
    }

    override fun getOldListSize(): Int {
        return oldList.size
    }

    override fun getNewListSize(): Int {
        return newList.size
    }

    /**
     * 用于局部更新
     */
    override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
        return super.getChangePayload(oldItemPosition, newItemPosition)
    }

}